package com.dlut.java5;

import java.util.Arrays;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: LiuHeng
 * @Description:
 * @Date: 2022-10-20 18:52
 */
public class T1020_XinYe {
    public static void main(String[] args) {
        T1020_XinYe s = new T1020_XinYe();
        String[] arr = new String[]{"2051 293","2051 293","2004 60","2022 2"};
        s.dateArraysSort(arr);
    }

    public String[] dateArraysSort (String[] dates) {
        // write code here
        Set<String> set = new HashSet<>();
        for (String temp : dates){
            set.add(temp);
        }
        dates = set.toArray(dates);
        int n = set.size();
        MyDate[] arrs = new MyDate[n];
        for (int i = 0; i < n; i++) {
            arrs[i] = new MyDate(dates[i].substring(0, 4), Integer.parseInt(dates[i].substring(5)));
        }
        Arrays.sort(arrs);
        System.out.println(Arrays.toString(arrs));
        String[] ans = new String[n];
        for (int i = 0; i < n; i++) {
            ans[i] = arrs[i].toString();
        }

        return ans;
    }

    public static final String[] DAY = new String[]{"1st","2nd","3rd","4th","5th","6th","7th","8th","9th","10th",
    "11st","12nd","13th","14th","15th","16th","17th","18th","19th","20th",
    "21st","22nd","23th","24th","25th","26th","27th","28th","29th","30th","31st"};

    public static final String[] MON = new String[]{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
    public static final int[] NUM = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
    public static final int[] RNUM = new int[]{31,29,31,30,31,30,31,31,30,31,30,31};

    class MyDate implements Comparable<MyDate>{
        String year;
        Integer day;
        public MyDate(String year, Integer day) {
            this.year = year;
            this.day = day;
        }

        @Override
        public int compareTo(MyDate o) {
            if (!year.equals(o.year)){
                return year.compareTo(o.year);
            }
            return day - o.day;
        }

        @Override
        public String toString() {
            int[] num = NUM;
            if (isRun())
                num = RNUM;
            int i = 0;
            int temp = day;
            for (; i < 12; i++) {
                if (temp > num[i]){
                    temp -= num[i];
                }else {
                    break;
                }
            }

            return DAY[temp-1] + " " + MON[i] + " " + year;
        }
        boolean isRun(){
            int y = Integer.parseInt(year);
            if (y%4 == 0 && y%100 != 0){
                return true;
            }
            return y % 400 == 0;
        }
    }
}
